<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsSerialLog extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['gl.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $goods_id = $this->request->post('goods_id');
            if(!empty($goods_id)){
                $where['gl.goods_id'] = ['in',$goods_id];
            }
            $serial = $this->request->post('serial');
            if(!empty($serial)){
                $where['gl.serial'] = ['in',$serial];
            }
            $stores_id = $this->request->post('stores_id');
            if(!empty($stores_id)){
                $where['gl.stores_id'] = ['in',$stores_id];
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_goods_serial_log')->alias('gl')
                    ->join('kuyou_goods g','g.id=gl.goods_id')
                    ->join('kuyou_stores s','s.id=gl.stores_id')
                    ->where($where)
                    ->page($page,$limit)
                    ->order('gl.serial asc,gl.createtime asc')
                    ->field('g.goods_code,g.goods_name,g.goods_specification_id,s.name as store_name,
                gl.id,gl.goods_id,gl.stores_id,gl.attribute_id,gl.serial,gl.business_type,gl.store_status,gl.remarks,gl.purchase_id,
                gl.purchase_code,FROM_UNIXTIME(gl.purchase_time,\'%Y-%m-%d\') as purchase_time,gl.customer_id,gl.order_id')
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_serial_log')->alias('gl')
                    ->join('kuyou_goods g','g.id=gl.goods_id')
                    ->join('kuyou_stores s','s.id=gl.stores_id')
                    ->where($where)
                    ->order('gl.serial asc,gl.createtime asc')
                    ->field('g.goods_code,g.goods_name,g.goods_specification_id,s.name as store_name,
                gl.id,gl.goods_id,gl.stores_id,gl.attribute_id,gl.serial,gl.business_type,gl.store_status,gl.remarks,gl.purchase_id,
                gl.purchase_code,FROM_UNIXTIME(gl.purchase_time,\'%Y-%m-%d\') as purchase_time,gl.customer_id,gl.order_id')
                    ->select();
            }
            $row_count = Db::name('kuyou_goods_serial_log')->alias('gl')
                ->join('kuyou_goods g','g.id=gl.goods_id')
                ->join('kuyou_stores s','s.id=gl.stores_id')
                ->where($where)->count();

            $listData = $list;
            $lists = array_reverse($list);
            $serial_id = '';
            foreach ($lists as $lists_k=>$lists_v){
                if($serial_id==$lists_v['serial']){
                    unset($lists[$lists_k]);
                }else{
                    $serial_id=$lists_v['serial'];
                }
            }
            foreach ($listData as $listData_k=>$listData_v){
                //供应商/客户
                if($listData_v['business_type']==2 || $listData_v['business_type']==3 || $listData_v['business_type']==8 || $listData_v['business_type']==9){
                    $listData[$listData_k]['customer_id'] = Db::name('kuyou_suppliers')->where(['id'=>$listData_v['customer_id']])->value('supplyer_name');
                }else if($listData_v['business_type']==4 || $listData_v['business_type']==5 || $listData_v['business_type']==10 || $listData_v['business_type']==11){
                    $listData[$listData_k]['customer_id'] = Db::name('kuyou_customers')->where(['id'=>$listData_v['customer_id']])->value('customer_name');
                }
                foreach ($lists as $lists_k=>$lists_v){
                    if($listData_v['id']!=$lists_v['id'] && $listData_v['serial']==$lists_v['serial']){
                        $listData[$listData_k]['store_status'] = '';
                    }
                }
            }

            $result = array("count" => $row_count, "list" => $listData);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
